const colors = [
    '#FF8C00', '#000000', '#33B5E5', '#FFB6C1', '#AFEEEE', '#9933CC', '#99CC00',
    '#DB7093', '#FF8800', '#FF4474', '#CC0000', '#ad8b3d', '#DC143C', '#8f4b4a',
    '#4B0089', '#FA8072', '#778899', '#00FFFF', '#1d953f', '#00FA9A', '#808000',
    '#DA70D6', '#845080', '#FF1493', '#7B68EE', '#ADFF2F', '#f69c9f', '#4a3113',
    '#9400D3', '#5F9EA0', '#7FFFAA', '#B0C4DE', '#F0F8FF', '#AFEEEE', '#D4F2E7',
    '#AA655C'
  ]
const bagColor = ['rgba(189,193,207,0.5)', 'rgba(255,248,220,0.5)', 'rgba(121,205,205,0.5)', 'rgba(132,112,255,0.5)']
const nameList = ['焙烧一燃烧一燃烧架一', '焙烧一燃烧一燃烧架二', '焙烧一燃烧一燃烧架三', '焙烧一燃烧一燃烧架四', '焙烧一排烟架一']
function getxAxis (xAxisData, pData) {
    let xAxis = [
      {
        type: 'category',
        boundaryGap: false,
        data: xAxisData,
        position: 'bottom',
        axisLabel: {
          margin: 10 // 标签到轴线的距离
        }
      }
    ]
    if (pData.length) {
      // TODO，设置P范围
    }
    let dataZoom = [{
      show: true,
      realtime: true,
      start: 0,
      height: 10,
      end: 70,
      xAxisIndex: [0]
    },
    {
      type: 'inside',
      realtime: true,
      start: 0,
      end: 70,
      xAxisIndex: [0]
    }
    ]
    return {xAxis, dataZoom}
  }

function getyAxis () {
    let yAxis = [
      {
        type: 'value',
        min: 'dataMin',
        max: 'dataMax',
        min: function (value) { // 取最小值向下取整为最小刻度
          let num = 10 * (value.min.toString().length - 2)
          return Math.floor(value.min / num) * num
        },
        max: function (value) { // 取最大值向上取整为最大刻度
          let num = 10 * (value.max.toString().length - 2)
          return Math.ceil(value.max / num) * num
        },
        splitNumber: 20,
        scale: true,
        splitLine: { // 网格线
          lineStyle: {
            type: 'dashed' // 设置网格线类型 dotted：虚线   solid:实线
          },
          show: true // 隐藏或显示
        },
        show: true,
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        minorTick: {
          show: false
        },
        axisLabel: {
          show: false
        }
      }
    ]
    return {yAxis}
  }

function getLineSeries (list, headerList) {
    let series = []
    let colorIndex = 0
    for (let i in list) {
      if (colorIndex >= colors.length) colorIndex = 0
      const color = colors[colorIndex]
      series.push({
        type: 'line',
        name: i,
        data: list[i],
        symbol: 'circle',
        showSymbol: false,
        symbolSize: 10,
        connectNulls: true,
        smooth: true,
        color: color,
        lineStyle: {
          normal: {
            width: 2,
            color: color
          }
        }
      })
      colorIndex++
    }
    return series
  }

function getBarSeries (list, headerList) {
    let series = []
    let colorIndex = 0
    for (let key in list) {
      if (colorIndex >= bagColor.length) colorIndex = 0
      const color = bagColor[colorIndex]
      series.push({
        name: key,
        type: 'bar',
        barGap: 0,
        barWidth: '100.2%',
        barCategoryGap: 0,
        data: list[key],
        itemStyle: {
          color: color
        }
      })
      colorIndex++
    }
    return series
  }

function getToolTip () {
    return {
      trigger: 'axis',
      confine: true,
      enterable: true, // 鼠标可以移入tooltip里
      extraCssText: 'max-height:400px;overflow:auto;font-size:12px;', // 向div一样设置最大高度,超出滚动
      position: function (point, params, dom, rect, size) {
        // 鼠标坐标和提示框位置的参考坐标系是：以外层div的左上角那一点为原点，x轴向右，y轴向下
        var x = 0 // x坐标位置
        var y = 0 // y坐标位置
        var pointX = point[0]
        var pointY = point[1]
        // 提示框大小
        var boxWidth = size.contentSize[0]
        var boxHeight = size.contentSize[1]
        if (pointX >= 96 && pointX < 1500) {
          x = pointX - boxWidth + 200
        } else if (pointX >= 1500) {
          x = pointX - boxWidth - 50
        }
        if (boxHeight > pointY) {
          y = 5
        } else { // 上边放得下
          y = pointY - boxHeight
        }
        return [x, y]
      },
      formatter: function (params) {
        let newParams = []
        let tooltipString = []
        newParams = [...params]
        newParams.sort((a, b) => {
          return b.value - a.value
        })
        var cont = '时间:' + params[0].axisValue + '<br/>'
        var res = null

        for (var i = 0; i < newParams.length; i++) {
          for (var c = 0; c < nameList.length; c++) {
            if (newParams[i] && newParams[i].seriesName === nameList[c]) {
              newParams.splice(i, 1)
            }
          }
        }

        newParams.forEach((p, i) => {
          res = p.marker + ' ' + p.seriesName + ': ' + p.value + '<br/>'
          tooltipString.push(res)
        })

        return cont + tooltipString.join('')
      }
    }
  }
export function getOptions(result){
    const {xAxis, dataZoom} = getxAxis(result.times, [])
    const {yAxis} = getyAxis()
    const barSeries = getBarSeries(result.seriesColor, result.list)
    const lineSeries = getLineSeries(result.series, result.list)
    const series = [].concat(lineSeries).concat(barSeries)
    const tooltip = getToolTip()
    const option = {
      backgroundColor: '#FFF',
      tooltip,
      color: bagColor,
      legend: {
        orient: 'horizontal',
        x: 'right', // 可设定图例在左、右、居中
        y: 'bottom', // 可设定图例在上、下、居中
        padding: [0, 100, 50, 0], // 可设定图例[距上方距离，距右方距离，距下方距离，距左方距离]
        itemGap: 10,
        // itemWidth: 5,
        data: result.list
        // selected: this.legend.selected
      },
      grid: {
        top: '3%',
        left: '10%',
        right: '10%',
        bottom: '30%',
        containLabel: true
      },
      toolbox: {
        feature: {
          saveAsImage: {
            show: false
          }
        }
      },
      xAxis,
      dataZoom,
      yAxis,
      series
    }
    return option
  }